package org.adream.account.rest.api;

import java.util.List;

import org.adream.account.entity.UserDetailEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.service.ADreamServiceException;
import org.adream.account.service.UserDetailService;
import org.adream.account.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户详细资料  controller
 * @author sl
 *
 */
@Controller
@RequestMapping("/user/detail/")
public class UserDetailApiController {
	
	@Autowired
	private UserDetailService userDetailService;

	@Value("${image.file.path.userpic}")
    private String userPicPath;  //用户图片
	
	/**
	 * 增
	 * @param userDetail
	 * @return
	 */
	@RequestMapping(value = "detail", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<String> addUserDetail(@RequestBody UserDetailEntity userDetail) {
		if(userDetail == null || StringUtils.isEmpty(userDetail.getUid())) {				
			return new ResultModel<String>(ResultModel.ERR_PARAM, "请求参数有误", null);
		}
		return userDetailService.addUserDetail(userDetail);
	}
	
	/**
	 * 删除(逻辑删除)
	 * @param uid user id
	 * @return
	 */
	@RequestMapping(value = "{uid}", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
	@ResponseBody
	public ResultModel<String> deleteUserDetail(@PathVariable("uid") String uid) {
		if(StringUtils.isEmpty(uid)) {				
			return new ResultModel<String>(ResultModel.ERR_PARAM, "请求参数有误", null);
		}
		return userDetailService.deleteUserDetail(uid);
	}
	
	/**
	 * 更新
	 * @param uid user id
	 * @param userDetail
	 * @return
	 */
	@RequestMapping(value = "{uid}", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
	@ResponseBody
	public ResultModel<?> updateUserDetail(@PathVariable("uid") String uid,@RequestBody UserDetailEntity userDetail) {
		if(StringUtils.isEmpty(uid) || userDetail == null) {				
			return new ResultModel<String>(ResultModel.ERR_PARAM, "请求参数有误", null);
		}
		try {
			return userDetailService.updateUserDetail(uid,userDetail);
		} catch (ADreamServiceException e) {
			return new ResultModel<String>(ResultModel.ERR_SYS, e.getMessage(), null);
		}
	}
	
	/**
	 * 根据use id查询userDetail
	 * @param uid user id
	 * @return
	 */
	@RequestMapping(value = "{uid}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<UserDetailEntity> queryUserDetailByUid(@PathVariable("uid") String uid) {
		if(StringUtils.isEmpty(uid)) {				
			return new ResultModel<UserDetailEntity>(ResultModel.ERR_PARAM, "请求参数有误", null);
		}
		UserDetailEntity userDetail = userDetailService.queryUserDetailByUid(uid);
		if(userDetail == null) {
			return new ResultModel<UserDetailEntity>(ResultModel.NOT_FOUND_OBJECT, "查询失败,用户不存在", null);
		}
		return new ResultModel<UserDetailEntity>(ResultModel.SUCCESS, "成功获取userDetail", userDetail);
	}
	
	/**
	 * 根据删除标志查询userDetail
	 * @param dr 删除标志
	 * @return
	 */
	@RequestMapping(value = "queryUserDetailsByDr", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> queryUserDetailsByDr(int dr) {
		List<UserDetailEntity> userDetails = userDetailService.queryUserDetailsByDr(dr);
		if(Utils.isEmptyCollection(userDetails)) {
			return new ResultModel<UserDetailEntity>(ResultModel.NOT_FOUND_OBJECT, "查询失败,用户不存在", null);
		}
		return new ResultModel<List<UserDetailEntity>>(ResultModel.SUCCESS, "成功获取userDetail", userDetails);
	}
}